//
//  MMTableView.m
//  EraPig
//
//  Created by Mac_Mini on 2021/2/24.
//

#import "MMTableView.h"
#import <objc/runtime.h>
@interface MMTableView ()

@property (nonatomic, copy) MMTBRefreshBlock pullRefreshBlock;
@property (nonatomic, copy) MMTBRefreshBlock loadRefreshBlock;

@end
static const char *PullRefreshBlockKey = "MMTableViewPullRefreshBlockKey";
static const char *LoadRefreshBlockKey = "MMTableViewLoadRefreshBlockKey";
@implementation MMTableView

/// 初始化TableView
/// @param frame frame
/// @param style 样式
/// @param dataSource 数据源
/// @param delegate 代理
- (instancetype)initWithFrame:(CGRect)frame
                        style:(UITableViewStyle)style
                   dataSource:(id)dataSource
                     delegate:(id)delegate{
    return [self initWithFrame:frame style:style dataSource:dataSource delegate:delegate separatorColor:nil];
}

/// 初始化TableView
/// @param frame frame
/// @param style 样式
/// @param dataSource 数据源
/// @param delegate 代理
/// @param separatorColor 分割线颜色
- (instancetype)initWithFrame:(CGRect)frame
                        style:(UITableViewStyle)style
                   dataSource:(id)dataSource
                     delegate:(id)delegate
               separatorColor:(UIColor *)separatorColor{
     if (self = [super initWithFrame:frame style:style]) {
        self.dataSource = dataSource;
        self.delegate = delegate;
        self.separatorColor = separatorColor;
        [self config];
    }
    return self;
}
- (void)config{
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.backgroundColor = [UIColor clearColor];
    self.tableFooterView = [UIView new];
    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
- (void)addPullRefresh:(MMTBRefreshBlock)pullRefreshBlock{
    if (self.mj_header) {
        return;
    }
    objc_setAssociatedObject(self, PullRefreshBlockKey, pullRefreshBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullRefreshAction)];
}
- (void)pullRefreshAction{
    MMTBRefreshBlock pullRefreshBlock = objc_getAssociatedObject(self, PullRefreshBlockKey);
    if (pullRefreshBlock) {
        pullRefreshBlock();
    }
}
- (void)addLoadRefresh:(MMTBRefreshBlock)loadRefreshBlock{
    if (self.mj_footer) {
        return;
    }
    objc_setAssociatedObject(self, LoadRefreshBlockKey, loadRefreshBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadRefreshAction)];
    [self.mj_footer endRefreshingWithNoMoreData];
    self.mj_footer.hidden = YES;
}
- (void)loadRefreshAction{
    MMTBRefreshBlock loadRefreshBlock = objc_getAssociatedObject(self, LoadRefreshBlockKey);
    if (loadRefreshBlock) {
        loadRefreshBlock();
    }
}
- (void)endRefresh{
    if (self.mj_header.isRefreshing) {
        [self.mj_header endRefreshing];
    }
    if (self.mj_footer.isRefreshing) {
        [self.mj_footer endRefreshing];
    }
}
- (void)setShowsLoadMoreScrolling:(BOOL)show{
    if (show) {
        [self.mj_footer resetNoMoreData];
        self.mj_footer.hidden = NO;
    }else{
        [self.mj_footer endRefreshingWithNoMoreData];
        self.mj_footer.hidden = YES;
    }
}

/// 加载更多是否隐藏
- (BOOL)footerRefresHiddenState{
    return self.mj_footer.hidden;
}

- (void)setNotDataView:(UIView *)defaultView{
    
    for (UIView *view in self.subviews) {
        if (view.tag == 123) {
            [view removeFromSuperview];
        }
    }
    
    BOOL hasData = NO;
    for (int i = 0; i < [self numberOfSections]; i++) {
        if ([self numberOfRowsInSection:i] > 0) {
            hasData = YES;
            break;
        }
    }
    if (CGRectEqualToRect(defaultView.frame, CGRectZero)) {
        defaultView.frame = self.bounds;
    }
    defaultView.tag = 123;
    [self addSubview:defaultView];
}

- (void)reloadData{
    [super reloadData];
    
    UIView *defaultView = [self viewWithTag:123];
    if (defaultView) {
        BOOL hasData = NO;
        for (int i = 0; i < self.numberOfSections; i++) {
            if ([self numberOfRowsInSection:i] > 0) {
                hasData = YES;
                break;
            }
        }
        if (hasData) {
            [defaultView removeFromSuperview];
        }
    }
}

@end
